window.onload = function () {
    getProduct();
}

var product;

document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("buy").addEventListener("click", function () {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://localhost:8080/buy", true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        var data = {
            id: product.id,
            username: null
        }

        data.username = getUserNameFromCookie();

        data.amount = parseInt(document.getElementById("number").innerHTML);

        console.log(data);

        xhr.send(JSON.stringify(data));

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    window.alert("购买成功");
                } else {
                    window.alert("提交失败");
                    console.error('请求失败，状态码：', xhr.status);
                }
            }
        };
    });

    document.getElementById("cart").addEventListener("click", function () {
        console.log(product);
        var data = {
            id: product.id,
            title: product.name,
            price: product.price,
            img: product.img,
            amount: 0
        }
        data.amount = parseInt(document.getElementById("number").innerHTML);

        var cart = JSON.parse(sessionStorage.getItem('cart')) || [];
        cart.push(data);
        sessionStorage.setItem('cart', JSON.stringify(cart));

        alert("加入购物车成功");
    });

    var num = document.getElementById("number");

    document.getElementById("minus").addEventListener("click", function () {
        var value = parseInt(num.innerHTML);
        value--;
        if (value < 1) {
            value = 1;
        }
        num.innerHTML = value.toString();
    });

    document.getElementById("add").addEventListener("click", function () {
        var value = parseInt(num.innerHTML);
        value++;
        if (value > 1000) {
            value = 1000;
            alert("最多购买1000件");
        }
        num.innerHTML = value.toString();
    });
})

function getProduct() {
    var xhr = new XMLHttpRequest();
    var url = window.location.href;
    var id = url.substring(url.lastIndexOf('id=') + 3);
    xhr.open("GET", "http://localhost:8080/getProduct?id=" + id, true);

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                showProduct(xhr.responseText);
            }
            else {
                console.error(xhr.status);
            }
        }
    }

    xhr.send();
}

function showProduct(json) {
    product = JSON.parse(json);
    const img = document.getElementsByClassName("img")[0];
    img.src = "img/" + product.img;
    const title = document.getElementsByClassName("title")[0];
    title.innerHTML = product.name;
    const price = document.getElementsByClassName("price")[0];
    price.innerHTML = "￥" + product.price;
    const sales = document.getElementsByClassName("sales")[0];
    sales.innerHTML = "已售" + product.sales;
    const stock = document.getElementsByClassName("stock")[0];
    stock.innerHTML = "剩" + product.stock + "件";
}